import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  AfterLoad
} from "typeorm"

import moment from "moment/moment"
import { Post } from "./Post";

moment.locale('zh-cn')

@Entity()
export class Photo {
  @PrimaryGeneratedColumn()
  id: number // 照片id

  @Column({ comment: "照片地址" })
  photoUrl?: string // 照片地址

  @ManyToOne(() => Post, { createForeignKeyConstraints: false })
  post: Post // 照片所属帖子

  @CreateDateColumn()
  createdAt: Date | string; // 照片发布时间

  @UpdateDateColumn()
  updatedAt: Date | string; // 照片更新时间

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}